package com.shushan.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.android.permission.FloatPermissionUtil;
import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;
import com.shushan.base.BaseActivity;

import static com.android.permission.rom.RomType.EMUI_ROM;
import static com.android.permission.rom.RomType.FLYME_ROM;
import static com.android.permission.rom.RomType.FUNTOUCHOS_ROM;
import static com.android.permission.rom.RomType.MIUI_ROM;
import static com.android.permission.rom.RomType.SAMSUNG_ROM;
import static com.android.permission.rom.RomType.SMARTISAN_ROM;

/**
 * @ClassName: OpenSettingUtils
 * @Desciption: 打开系统设置界面工具类
 * @author: yichaohua
 * @date: 2018-04-13
 */
public class OpenSettingUtils {

    private static final String TAG = "OpenSettingUtils";

    public static void openUsageSetting(Context context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void openUsageSetting(BaseActivity context){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                context.setAllowBack(true);
                Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception e) {
                context.setAllowBack(false);
                e.printStackTrace();
            }
        }
    }

    public static void openDeveloperSetting(final BaseActivity context) {
        context.setAllowBack(true);
        Intent intent = new Intent("android.settings.APPLICATION_DEVELOPMENT_SETTINGS");
        context.startActivity(intent);
    }

    public static void openHuaWeiPowerSetting(Context context) {
        if(RomUtils.getEmuiLeval(context) >= 14){
            context.startActivity(new Intent(Settings.ACTION_SETTINGS));
        }else {
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.huawei.systemmanager",
                        "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception ex) {
                context.startActivity(new Intent(Settings.ACTION_SETTINGS));
                ex.printStackTrace();
            }
        }

    }

    public static void openHuaWeiPowerSetting(BaseActivity context) {
        if(RomUtils.getEmuiLeval(context) >= 14){
            openSystemMainSetting(context);
        }else {
            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.huawei.systemmanager",
                        "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception ex) {
                openSystemMainSetting(context);
                ex.printStackTrace();
            }
        }

    }

    public static void openFloatManager(BaseActivity context) {
        context.setAllowBack(true);

        RomType romType = RomUtils.getRomType(context);
        if(romType == FUNTOUCHOS_ROM){
            openVivoManager(context);
        }else if (romType == SMARTISAN_ROM){
            openSmartPermission(context);
        } else {
            FloatPermissionUtil.applyPermission(true,context);
        }

    }

    public static void openFloatManager(Context context) {
        RomType romType = RomUtils.getRomType(context);
        if(romType == FUNTOUCHOS_ROM){
            openVivoManager(context);
        }else if (romType == SMARTISAN_ROM){
            openSmartPermission(context);
        } else {
            FloatPermissionUtil.applyPermission(true,context);
        }

    }

    public static void openVivoBackground(BaseActivity context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                    "com.vivo.permissionmanager.activity.PurviewTabActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            openSystemMainSetting(context);
            ex.printStackTrace();
        }

    }


    public static void openVivoManager(BaseActivity context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.iqoo.secure",
                    "com.iqoo.secure.MainActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();

            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                        "com.vivo.permissionmanager.activity.PurviewTabActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (Exception ex) {
                openSystemMainSetting(context);
                ex.printStackTrace();
            }
        }

    }

    public static void openVivoManager(Context context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.iqoo.secure",
                    "com.iqoo.secure.MainActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openVivoPowerSetting(Context context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.vivo.abe",
                    "com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);

        } catch (Exception ex) {
            ex.printStackTrace();

            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.iqoo.powersaving",
                        "com.iqoo.powersaving.PowerSavingManagerActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();

                context.startActivity(new Intent(Settings.ACTION_SETTINGS));
            }
        }
    }

    public static void openVivoPowerSetting(BaseActivity context) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.vivo.abe",
                    "com.vivo.applicationbehaviorengine.ui.ExcessivePowerManagerActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();

            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.iqoo.powersaving",
                        "com.iqoo.powersaving.PowerSavingManagerActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }catch (Exception e){
                e.printStackTrace();

                openSystemMainSetting(context);
            }
        }
    }

    public static void openPowerSetting(Context context) {
        RomType romType = RomUtils.getRomType(context);
        Log.i(TAG,"romType=" + romType);
        if(romType == MIUI_ROM){
            XiaomiDeviceUtil.toConfigApp(context,context.getPackageName(),"书山宝");
        }else if(romType == EMUI_ROM){
            openHuaWeiPowerSetting(context);
        }else if(romType == FUNTOUCHOS_ROM){
            openVivoPowerSetting(context);
        }else if(romType == SMARTISAN_ROM){
            openSmartMemory(context);
        } else if (romType == FLYME_ROM){
            openFlymePower(context);
        } else if (romType == SAMSUNG_ROM){
            openSamsungSmat(context);
        } else {
            try {
                context.startActivity(new Intent(Intent.ACTION_POWER_USAGE_SUMMARY));
            }catch (Exception e){
                context.startActivity(new Intent(Settings.ACTION_SETTINGS));
                e.printStackTrace();
            }
        }
    }

    public static void openPowerSetting(BaseActivity context) {
        context.setAllowBack(true);

        RomType romType = RomUtils.getRomType(context);
        Log.i(TAG,"romType=" + romType);
        if(romType == MIUI_ROM){
            XiaomiDeviceUtil.toConfigApp(context,context.getPackageName(),"书山宝");
        }else if(romType == EMUI_ROM){
            openHuaWeiPowerSetting(context);
        }else if(romType == FUNTOUCHOS_ROM){
            openVivoPowerSetting(context);
        }else if(romType == SMARTISAN_ROM){
            openSmartMemory(context);
        } else if (romType == FLYME_ROM){
            openFlymePower(context);
        } else if (romType == SAMSUNG_ROM){
            openSamsungSmat(context);
        }else {
            try {
                Intent intent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }catch (Exception e){
                openSystemMainSetting(context);
                e.printStackTrace();
            }
        }

    }

    public static void openSystemLockSetting(BaseActivity context) {
        Intent intent = new Intent("android.app.action.SET_NEW_PASSWORD");
        context.setAllowBack(true);
        try {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }catch (Exception e){
            context.startActivity(new Intent(Settings.ACTION_SETTINGS));
        }

    }

    public static void openSystemMainSetting(final BaseActivity context) {
        context.setAllowBack(true);

        try {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }catch (Exception e){
            context.setAllowBack(false);
            e.printStackTrace();
        }

    }

    public static void openSystemDesktopSetting(BaseActivity context) {
        context.setAllowBack(true);

        try {
            context.startActivity(new Intent(Settings.ACTION_HOME_SETTINGS));
        }catch (Exception e){
            context.setAllowBack(false);
            e.printStackTrace();
        }

    }

    public static void openSecuritySetting(BaseActivity context) {
        context.setAllowBack(true);
        Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
        context.startActivity(intent);
    }

    public static void gotoSystemLockSetting(final BaseActivity context){
        RomType romType = RomUtils.getRomType(context);
        if(romType == MIUI_ROM){
            context.setAllowBack(true);
            context.startActivity(new Intent(Settings.ACTION_SETTINGS));
        }else {
            openSystemLockSetting(context);
        }
    }

    public static void openSmartPermission(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.smartisanos.security",
                    "com.smartisanos.security.PermissionsActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openSmartMemory(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.android.settings",
                    "com.android.settings.applications.ManagerApplicationsRunningActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openFlymePower(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.meizu.safe",
                    "com.meizu.safe.permission.SmartBGActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openSamsungSmat(Context context) {
        try {
            Intent intent = new Intent();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                intent.setComponent(new ComponentName("com.samsung.android.sm_cn",
                        "com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity"));
            }else {
                intent.setComponent(new ComponentName("com.samsung.android.sm",
                        "com.samsung.android.sm.app.dashboard.SmartManagerDashBoardActivity"));
            }

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
